//
//  WJRealmUtils.m
//  JerryIos
//
//  Created by 王军 on 2018/2/16.
//  Copyright © 2018年 王军. All rights reserved.
//

#import "WJRealmUtils.h"
#import "WJRealmData.h"

@implementation WJRealmUtils

//static作用：保证realm这个变量，只能被WJRealmUtils.m直接访问
static RLMRealm *realm;

+(void)initialize {
    //构建数据库
    
    //第一种方式:默认Realm数据库
//    realm = [RLMRealm defaultRealm];
    
    //第二种方式:自定义Realm数据库
    //获得沙盒中数据库文件名
    NSString *fileName = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"myrealm.realm"];
    
    NSLog(@"数据库文件路径==%@",fileName);
    
    //Realm配置
    RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];
    config.fileURL = [NSURL URLWithString:fileName];
    config.objectClasses = @[WJRealmData.class];
    config.readOnly = NO;
    int currentVersion = 1.0;
    config.schemaVersion = currentVersion;
    // 这里是设置数据迁移的block
    config.migrationBlock = ^(RLMMigration *migration , uint64_t oldSchemaVersion) {
        if (oldSchemaVersion < currentVersion) {
            NSLog(@"oldSchemaVersion < currentVersion");
        }
    };
    [RLMRealmConfiguration setDefaultConfiguration:config];
    //创建数据库
    realm = [RLMRealm realmWithConfiguration:config error:nil];
    
}

+(void) addData:(WJRealmData *)data {
    [realm beginWriteTransaction];
    [realm addObject:data];
    [realm commitWriteTransaction];
}

+(void) updateData:(WJRealmData *)data {
    [realm transactionWithBlock:^{
        [realm addOrUpdateObject:data];
    }];
}

+(void) deleteData:(WJRealmData *)data {
    [realm beginWriteTransaction];
    [realm deleteObject:data];
    [realm commitWriteTransaction];
}




+(void) addDataArray:(NSArray *)dataArray {
    [realm transactionWithBlock:^{
        [realm addOrUpdateObjects:dataArray];
    }];
}

+(void) updateDataArray:(NSArray *)dataArray {
    [realm transactionWithBlock:^{
        [realm addOrUpdateObjects:dataArray];
    }];
}

+(void) deleteDataArray:(NSArray *)dataArray {
    [realm transactionWithBlock:^{
        [realm deleteObjects:dataArray];
    }];
}



//获得当前所有数据
+(NSArray *) data {
    RLMResults *dataArray = [WJRealmData allObjects];
    NSMutableArray *mutDataArray = [NSMutableArray array];
    for (WJRealmData *data in dataArray) {
        [mutDataArray addObject:data];
    }
    return mutDataArray;
}

+(WJRealmData *) dataById:(int)id {
    //第一种方式
    NSString *whereSql = [NSString stringWithFormat:@"id=%i",id];
    RLMResults *dataArray = [WJRealmData objectsWhere:whereSql];
    //第二种方式：使用谓词
//    NSString *whereSql = @"id=?";
//    RLMResults *dataArray = [WJRealmData objectsWhere:whereSql, @(id)];
    WJRealmData *data = [dataArray objectAtIndex:0];
    
    return data;
}

+(int) getMaxId {
    RLMResults *dataArray = [WJRealmData allObjects];
    int maxId = 0;
    for (WJRealmData *data in dataArray) {
        if (maxId < data.id) {
            maxId = data.id;
        }
    }
    return maxId;
}

@end
